<template>
  <div class="admin-home">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover" v-loading="loading">
          <div slot="header">
            <span>商家总数</span>
          </div>
          <div class="card-content">
            <span class="number">{{ dashboardData.merchantCount }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" v-loading="loading">
          <div slot="header">
            <span>骑手总数</span>
          </div>
          <div class="card-content">
            <span class="number">{{ dashboardData.riderCount }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" v-loading="loading">
          <div slot="header">
            <span>用户总数</span>
          </div>
          <div class="card-content">
            <span class="number">{{ dashboardData.userCount }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" v-loading="loading">
          <div slot="header">
            <span>平台总收入</span>
          </div>
          <div class="card-content">
            <span class="number">¥{{ dashboardData.totalRevenue ? dashboardData.totalRevenue.toFixed(2) : '0.00' }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" style="margin-top: 20px">
      <el-col :span="12">
        <el-card shadow="hover" v-loading="loading">
          <div slot="header">
            <span>待审核统计</span>
          </div>
          <div class="stat-container">
            <div class="stat-item">
              <span class="stat-label">待审核商家</span>
              <span class="stat-value">{{ dashboardData.pendingMerchantCount }}</span>
              <el-button type="text" @click="goToPage('/admin/merchant/audit')">查看</el-button>
            </div>
            <div class="stat-item">
              <span class="stat-label">待审核骑手</span>
              <span class="stat-value">{{ dashboardData.pendingRiderCount }}</span>
              <el-button type="text" @click="goToPage('/admin/rider/audit')">查看</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover">
          <div slot="header">
            <span>近7日订单统计</span>
          </div>
          <!-- 订单统计图表 -->
          <div id="order-chart" class="chart-container" v-loading="loading"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getDashboardData } from '@/api/admin';
import * as echarts from 'echarts';

export default {
  name: 'AdminHome',
  data() {
    return {
      dashboardData: {
        merchantCount: 0,
        riderCount: 0,
        userCount: 0,
        totalRevenue: 0,
        pendingMerchantCount: 0,
        pendingRiderCount: 0,
        orderStats: []
      },
      loading: false,
      orderChart: null
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    // 销毁图表实例
    if (this.orderChart) {
      this.orderChart.dispose();
      this.orderChart = null;
    }
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    goToPage(path) {
      this.$router.push(path);
    },
    
    fetchDashboardData() {
      this.loading = true;
      
      getDashboardData()
        .then(res => {
          if (res.code === 200) {
            this.dashboardData = res.data;
            this.$nextTick(() => {
              this.initOrderChart();
            });
          } else {
            this.$message.error(res.message || '获取仪表盘数据失败');
          }
        })
        .catch(err => {
          console.error('获取仪表盘数据失败', err);
          this.$message.error('获取仪表盘数据失败');
        })
        .finally(() => {
          this.loading = false;
        });
    },
    
    initOrderChart() {
      if (!this.dashboardData.orderStats || this.dashboardData.orderStats.length === 0) {
        return;
      }
      
      // 初始化图表
      if (!this.orderChart) {
        this.orderChart = echarts.init(document.getElementById('order-chart'));
      }
      
      // 准备数据
      const dates = this.dashboardData.orderStats.map(item => item.date);
      const orderCounts = this.dashboardData.orderStats.map(item => item.orderCount);
      const amounts = this.dashboardData.orderStats.map(item => item.amount);
      
      // 设置选项
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['订单数量', '交易金额']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: dates
        },
        yAxis: [
          {
            type: 'value',
            name: '订单数量',
            position: 'left'
          },
          {
            type: 'value',
            name: '交易金额',
            position: 'right',
            axisLabel: {
              formatter: '¥{value}'
            }
          }
        ],
        series: [
          {
            name: '订单数量',
            type: 'bar',
            data: orderCounts
          },
          {
            name: '交易金额',
            type: 'line',
            yAxisIndex: 1,
            data: amounts
          }
        ]
      };
      
      // 渲染图表
      this.orderChart.setOption(option);
    },
    
    handleResize() {
      if (this.orderChart) {
        this.orderChart.resize();
      }
    }
  },
  created() {
    this.fetchDashboardData();
  }
}
</script>

<style scoped>
.admin-home {
  padding: 20px;
}
.card-content {
  text-align: center;
}
.number {
  font-size: 28px;
  font-weight: bold;
  color: #409EFF;
}
.stat-container {
  padding: 10px 0;
}
.stat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}
.stat-item:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}
.stat-label {
  color: #606266;
}
.stat-value {
  color: #409EFF;
  font-weight: bold;
  font-size: 20px;
}
.chart-container {
  height: 300px;
}
</style> 